package cn.iocoder.yudao.module.member.api.user;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.MemberUserPageReqVO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;

/**
 * 会员用户的 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberUserApiImpl implements MemberUserApi {

    @Resource
    private MemberUserService userService;

    @Override
    public MemberUserRespDTO getUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        return MemberUserConvert.INSTANCE.convert2(user);
    }

    /**
     * 获得会员用户信息
     *
     * @param userToken 用户令牌
     * @return 用户信息
     */
    @Override
    public MemberUserRespDTO getUserByUserToken(String  userToken) {
        MemberUserDO user = userService.getUserByUserToken(userToken);
        return MemberUserConvert.INSTANCE.convert2(user);
    }

    @Override
    public List<MemberUserRespDTO> getUserList(Collection<Long> ids) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserList(ids));
    }

    @Override
    public List<MemberUserRespDTO> getUserListByNickname(String nickname) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserListByNickname(nickname));
    }

    @Override
    public MemberUserRespDTO getUserByMobile(String mobile) {
        return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
    }

    @Override
    public void validateUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        if (user == null) {
            throw exception(USER_MOBILE_NOT_EXISTS);
        }
    }

    /**
     * 查询用户数量（启用中）
     *
     * @return
     */
    @Override
    public Long count() {
        return userService.count();
    }

    /**
     * 【管理员】获得会员用户分页
     *
     * @param pageNo
     * @param pageSize
     * @return 会员用户分页
     */
    @Override
    public List<String> selectListEmail(Integer pageNo, Integer pageSize) {
        MemberUserPageReqVO pageReqVO = new MemberUserPageReqVO();
        pageReqVO.setPageNo(pageNo);
        pageReqVO.setPageNo(pageSize);
        PageResult<MemberUserDO> userPage = userService.getUserPage(pageReqVO);
        return userPage.getList().stream().map(MemberUserDO::getEmail).collect(Collectors.toList());
    }

}
